package com.dingreading.cloud.app.service.impl;

import com.dingreading.cloud.app.entity.Books;
import com.dingreading.cloud.app.entity.table.BooksTableDef;
import com.dingreading.cloud.app.mapper.BooksMapper;
import com.dingreading.cloud.app.service.BooksService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.mybatisflex.core.query.QueryMethods.max;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class BooksServiceImpl extends BaseServiceImpl<BooksMapper, Books, BooksTableDef> implements BooksService {

    @Override
    protected BooksTableDef getTable() {
        return BooksTableDef.books;
    }

    @Resource
    private BooksMapper booksMapper;


    /**
     * 按isbn或书签号获取书籍
     *
     * @return Books
     */
    @Override
    public List<Books> getByIsbnOrBookNum(String isbn) {
        return getMapper().selectListByCondition(table.isbn.eq(isbn).or(table.bookNum.eq(isbn)));
    }

    @Override
    public List<Books> getByIsbnOrBookNumOrName(String isbn) {
//        return getMapper().selectListByCondition(table.isbn.like(isbn).or(table.bookNum.like(isbn)).or(table.bookName.like(isbn)));

        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.isbn.like(isbn)
                .or(table.bookNum.like(isbn))
                .or(table.bookName.like(isbn))
        );

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .limit(0, 10);

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<Books> getByIsbnOrBookNumNotInId(String isbn, List<Long> bookIds) {
        QueryCondition condition = table.isbn.eq(isbn).or(table.bookNum.eq(isbn));
        if (CollectionUtils.isNotEmpty(bookIds))
            condition.and(table.id.notIn(bookIds));

        return getMapper().selectListByCondition(condition);
    }

    /**
     * 获取书号最大的
     *
     * @return Books
     */
    @Override
    public Books getMaxBookNum() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(max(table.id))
                .from(table);

        return getMapper().selectOneByCondition(table.id.eq(wrapper));
    }

    @Override
    public Page<Books> pageList(PageUtil pageUtil, String keyword, Integer status) {

        QueryCondition condition = QueryCondition.createEmpty();
        if (null != status)
            condition.and(table.status.eq(status));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.bookName.like(keyword)
                    .or(table.isbn.like(keyword))
                    .or(table.bookNum.like(keyword))
                    .or(table.author.like(keyword))
                    .or(table.press.like(keyword)));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.id, table.bookName, table.isbn, table.bookNum, table.author, table.bookLabelId,
                        table.bookLabelName, table.unitPrice, table.press, table.wordCount, table.source, table.status,
                        table.editTime)
                .from(table)
                .where(table.img.isNotNull())
                .orderBy(table.id.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

}
